import request from './request.js'
import request2 from './request2.js'
// import { resolve } from 'dns'

// 假轮播
const getBannerlist = (type) => {
  type = type || 'home'
  return new Promise(resolve => {
    request.get('/banner', { params: { type } }).then(res => {
      resolve(res.data)
    })
  })
}
// 假地址
const orderAll = (userid) => {
  return new Promise(resolve => {
    request.get('/order/all', { params: { userid } }).then((data) => {
      resolve(data.data)
    })
  })
}

const orderTo = (userid, flag) => {
  return new Promise(resolve => {
    request.get('/order/all', { params: { userid, flag } }).then((data) => {
      resolve(data.data)
    })
  })
}

// 上方是mock假数据 用的request，下方用的request2

const getCode = (tel) => {
  return new Promise(resolve => {
    request2.post('/admin', { tel }).then(res => {
      resolve(res.data)
    })
  })
}
const addUser = (tel, password, username) => {
  return new Promise(resolve => {
    request2.post('/admin/add', { tel, password, username }).then(res => {
      resolve(res.data)
    })
  })
}
const findTel = (obj) => {
  return new Promise(resolve => {
    request2.post('/admin/find', obj).then(res => {
      resolve(res.data)
    })
  })
}
const updatePassword = (tel, password) => {
  return new Promise(resolve => {
    request2.post('/admin/update', { tel, password }).then(res => {
      resolve(res.data)
    })
  })
}
const getCode2 = (tel) => {
  return new Promise(resolve => {
    request2.post('/admin/code', { tel }).then(res => {
      resolve(res.data)
    })
  })
}
const isLogin = () => {
  return new Promise(resolve => {
    request2.post('/admin/islogin').then(res => {
      resolve(res.data)
    })
  })
}
const uploadAvatar = (avatar, userid) => {
  return new Promise(resolve => {
    request2.post('/admin/updateAvatar', { avatar, userid }).then(() => {
      resolve()
    })
  })
}

const kind = (classifyCode) => {
  return new Promise(resolve => {
    request2.post('/kind', { classifyCode }).then((data) => {
      resolve(data)
    })
  })
}

const echartAdd = (obj) => {
  return new Promise(resolve => {
    request2.post('/echarts/add', obj).then(() => {
      resolve()
    })
  })
}
const kindAll = () => {
  return new Promise(resolve => {
    request2.post('/kind/all').then((data) => {
      resolve(data)
    })
  })
}
const proall = () => {
  return new Promise(resolve => {
    request2.post('/proall').then(res => {
      resolve(res.data[0])
    })
  })
}
const echartFind = (userid) => {
  console.log(userid)
  return new Promise(resolve => {
    request2.get('/echarts/find', { params: { userid } }).then((data) => {
      resolve(data)
    })
  })
}
const kindDetail = (kindid) => {
  return new Promise(resolve => {
    request2.get('/kind/detail', { params: { kindid } }).then((data) => {
      resolve(data)
    })
  })
}
const kindNav = () => {
  return new Promise(resolve => {
    request2.get('/kind/nav').then(data => {
      resolve(data)
    })
  })
}
const address = (userid, flag) => {
  return new Promise(resolve => {
    request2.get('/address', { params: { userid, flag } }).then(res => {
      resolve(res)
    })
  })
}
const bannerImg = () => {
  return new Promise(resolve => {
    request2.get('/bannerimg/find').then(res => {
      resolve(res)
    })
  })
}
const proFind = (userid) => {
  return new Promise(resolve => {
    request2.get('/cart2', { params: { userid } }).then(res => {
      resolve(res)
    })
  })
}
const prodelete = (cartid) => {
  return new Promise(resolve => {
    request2.get('/cart2/delete', { params: { cartid } }).then(res => {
      resolve(res)
    })
  })
}
const proupdate = (cartid, num) => {
  return new Promise(resolve => {
    request2.post('/cart2/update', { cartid, num }).then(res => {
      resolve(res)
    })
  })
}
const getSearch = (text) => {
  return new Promise(resolve => {
    request2.get('/kind/search', { params: { text } }).then(res => {
      resolve(res)
    })
  })
}
const proDetail = (userid, kindid, num) => {
  // console.log(userid)
  return new Promise(resolve => {
    request2.post('/cart2/add', { userid, kindid, num }).then(res => {
      resolve(res)
    })
  })
}
const orderUpate0 = (orderid) => {
  return new Promise(resolve => {
    request2.get('/order/update0', { params: { orderid } }).then(res => {
      resolve(res)
    })
  })
}
const orderUpate1 = (orderid) => {
  return new Promise(resolve => {
    request2.get('/order/update1', { params: { orderid } }).then(res => {
      resolve(res)
    })
  })
}
const orderUpate2 = (orderid) => {
  return new Promise(resolve => {
    request2.get('/order/update2', { params: { orderid } }).then(res => {
      resolve(res)
    })
  })
}
const orderUpate3 = (orderid) => {
  return new Promise(resolve => {
    request2.get('/order/update3', { params: { orderid } }).then(res => {
      resolve(res)
    })
  })
}
const orderFind = (userid, status) => {
  return new Promise(resolve => {
    request2.get('/order/find', { params: { userid, status } }).then(res => {
      resolve(res)
    })
  })
}
const orderAdd = (userid, list) => {
  return new Promise(resolve => {
    request2.get('/order/add', { params: { userid, status } }).then(res => {
      resolve(res)
    })
  })
}
export {
  getBannerlist,
  getCode,
  addUser,
  findTel,
  updatePassword,
  getCode2,
  isLogin,
  uploadAvatar,
  kind,
  orderAll,
  proFind,
  prodelete,
  proupdate,
  orderTo,
  echartAdd,
  proall,
  echartFind,
  kindDetail,
  kindAll,
  kindNav,
  address,
  bannerImg,
  getSearch,
  proDetail,
  orderUpate0,
  orderUpate1,
  orderUpate2,
  orderUpate3,
  orderFind,
  orderAdd

}
